package yxy.cherry.game.base.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

abstract public class MqMessage {
	static public void sendMessage(MqMessage mqMessage) {
		String str = JSONObject.toJSONString(mqMessage);
		MqManager.getInstance().send(mqMessage.tags(), str.getBytes());
	}

	@SuppressWarnings("unchecked")
	static public <M extends MqMessage> M decodeMessage(byte[] body) throws Exception {
		String str = new String(body);
		JSONObject jobj = JSONObject.parseObject(str);
		Class<?> clazz = Class.forName(jobj.getString("type"));
		return (M) JSON.parseObject(str, clazz);
	}

	public Class<?> getType() {
		return this.getClass();
	}

	abstract public String tags();
}
